package com.scau.ums.controller;

import com.github.pagehelper.PageInfo;
import com.scau.ums.mapper.NewsMapper;
import com.scau.ums.model.News;
import com.scau.ums.service.imp.news.newsImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
//@RequestMapping(value="/readNews")
public class readNewsController {

    //翻页Service
    @Autowired
    private newsImp newsImp;
    @Autowired
    private NewsMapper newsMapper;



    //展示当前页新闻
    @RequestMapping(value="/showNews")
    public  String  showNews(HttpServletRequest request, @RequestParam(value = "currentPage",required=false,defaultValue="1")Integer currentPage){

        List<News> NewsList = newsImp.SelectNews(currentPage);
        PageInfo<News> pageInfo= new PageInfo<News>(NewsList);
        request.setAttribute("NewsList",NewsList);
        request.setAttribute("pageInfo",pageInfo);

        return "showNews";
    }


    //获取特定的新闻对象，即在新闻列表中点击的新闻对象
    @RequestMapping(value="/specifiedNews")
    public String specifiedNews(HttpServletRequest request, int NewsId){
        News specifiedNews = newsMapper.selectByPrimaryKey(NewsId);
        request.setAttribute("specifiedNews",specifiedNews);
        return "readNews";
    }

    @RequestMapping(value = "ChangePage")
    @ResponseBody
    public List<News> ChangePage(@RequestParam(value = "currentPage",required=false,defaultValue="1")Integer currentPage){
        List<News> NewsList = newsImp.SelectNews(currentPage);
        PageInfo<News> pageInfo= new PageInfo<News>(NewsList);
        NewsList.size();
        return NewsList;
    }

}
